// pages/cleanHisTask/cleanHisTask.js
const httprequest = require('../../utils/http_utils.js')
const common = require('../../utils/common.js')
const dateutil = require('../../utils/date_util.js')
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '历史任务',
    tabTxt: ['位置', '状态'],
    tab: [false, false, false],
    locationList: [],
    stateList: [{
        id: '1',
        state: '已保洁'
      },
      {
        id: '2',
        state: '已评价'
      },
      {
        id: '3',
        state: '已逾期'
      },
    ],
    maskFlag: false, //是否显示遮罩
    tasklist: [],
    totalNum: 0, //任务总数
    PageSize: 10,
    CurrentPage: 1,
    RoomGuid: '', //房间筛选选中值
    TaskStatus: '0', //保洁筛选选中状态
    Starttime: '', //查询开始时间
    Endtime: '', //查询开始时间
    TaskName: '', //保洁任务名称
    btn_width: '15%',
    btn_text: '本月',
    showzanwu: false,
    customHeight: 0, //自定义高度
  },

  //位置状态选项点击事件
  searchchecked: function(e) {
    if (e.currentTarget.dataset.condition == 'location') {
      //位置多选
      var check_arr = this.data.locationList
      check_arr[e.currentTarget.dataset.index].checked = !check_arr[e.currentTarget.dataset.index].checked
      this.setData({
        locationList: check_arr
      })
    } else if (e.currentTarget.dataset.condition == 'state') {
      //状态单选
      this.setData({
        TaskStatus: e.currentTarget.dataset.id
      })
    }
  },

  //隐藏遮罩
  hidemask() {
    this.setData({
      tab: [false, false, false],
      maskFlag: false,
    })
  },

  //重置
  reset(e) {
    if (e.currentTarget.dataset.type == 'loc') {
      var arr = this.data.locationList
      for (var i = 0; i < arr.length; i++) {
        arr[i].checked = false
      }
      this.setData({
        locationList: arr,
        RoomGuid: ''
      })
    } else if (e.currentTarget.dataset.type == 'state') {
      this.setData({
        TaskStatus: '0'
      })
    }
  },

  //确定
  confirm(e) {
    console.log(e)
    this.setData({
      tab: [false, false, false],
      maskFlag: false,
      tasklist: []
    })
    var RoomGuid = ''
    if (e.currentTarget.dataset.type == 'loc') {
      var arr = this.data.locationList
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
          RoomGuid += arr[i].RowGuid + ','
        }
      }
      if (RoomGuid.indexOf(',') != -1) {
        RoomGuid = RoomGuid.substring(0, RoomGuid.lastIndexOf(','))
      }
      this.setData({
        locationList: arr,
        RoomGuid: RoomGuid
      })
      console.log(this.data.RoomGuid)
    }
    this.getHisTaskList();
  },

  //获取历史保洁任务
  getHisTaskList() {
    var that = this
    var param = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      RoomGuid: that.data.RoomGuid, //房间筛选选中值
      TaskStatus: that.data.TaskStatus, //保洁筛选选中状态
      Starttime: that.data.Starttime, //查询开始时间
      Endtime: that.data.Endtime, //查询开始时间
      TaskName: that.data.TaskName, //保洁任务名称
    }
    console.log(param)
    httprequest.GetValidateRequest(httprequest.apiInfo.GetHistoryCleanTaskListMobileApi, param,
      function(res) {
        console.log('历史保洁任务', res)
        if (res.totalNum != 0) {
          let temparry = that.data.tasklist
          for (var i = 0; i < res.pageData.length; i++) {
            var param = {
              RowGuid: res.pageData[i].RowGuid,
              CleanName: res.pageData[i].CleanName,
              Tasklocation: res.pageData[i].Tasklocation,
              State: res.pageData[i].State,
              CleanEndedTime: res.pageData[i].CleanEndedTime == null ? '' : dateutil.dateFormat2(res.pageData[i].CleanEndedTime),
              State: res.pageData[i].State,
            }
            temparry.push(param)
          }
          that.setData({
            totalNum: res.totalNum,
            tasklist: temparry,
            showzanwu: false
          })
        } else {
          that.setData({
            showzanwu: true
          })
        }
      },
      function(err) {
        console.error(err)
        that.setData({
          showzanwu: true
        })
      }
    )
  },

  //上拉加载
  scrollToLower: function() {
    var that = this
    // console.log(that.data.totalNum + "_______总条数")
    var pages = Math.round((that.data.totalNum / that.data.PageSize) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        CurrentPage: that.data.CurrentPage + 1
      })
      that.getHisTaskList();
    }
  },


  //位置状态点击事件
  filterTab: function(e) {
    var data = [false, false, false];
    var index = e.currentTarget.dataset.index;
    data[index] = !this.data.tab[index];
    this.setData({
      tab: data,
      maskFlag: true,
    })
  },

  pickdate() {
    wx.navigateTo({
      url: '/pages/timeData/timeData?frompage=baojie',
    })
  },

  taskdetail(e) {
    console.log(e.currentTarget.dataset.rowguid)
    wx.navigateTo({
      url: '/pages/cleanTaskdetail/cleanTaskdetail?TaskRowGuid=' + e.currentTarget.dataset.rowguid,
    })
  },

  //获取历史保洁任务房间列表
  GetHistoryCleanRoom() {
    var that = this
    httprequest.GetValidateRequest(httprequest.apiInfo.GetHistoryCleanRoomApi, null,
      function(res) {
        console.log('历史保洁任务房间列表', res)
        if (res != null && res.Localdata.length > 0) {
          let temp = that.data.locationList
          for (var i = 0; i < res.Localdata.length; i++) {
            var param = {
              RowGuid: res.Localdata[i].RowGuid,
              Tasklocation: res.Localdata[i].Tasklocation,
              checked: false
            }
            temp.push(param)
          }
          that.setData({
            locationList: temp
          })
        }
      },
      function(err) {
        console.error(err)
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      customHeight: (51 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2
    })
    console.log('---------导航栏高度--------' + this.data.customHeight)

    this.timePicker = this.selectComponent("#timePicker");
    this.refreshView = this.selectComponent("#refreshView");

    this.GetHistoryCleanRoom();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //选择日期后返回重新抓取数据
    console.log('Starttime', this.data.Starttime)
    console.log('Endtime', this.data.Endtime)
    this.setData({
      tasklist: [],
      PageSize: 10, //单页数据条数
      CurrentPage: 1, //当前页码
    })
    this.getHisTaskList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      console.log("scrollToUpper")
      var that = this
      that.setData({
        PageSize: that.data.PageSize,
        CurrentPage: 1,
        RoomGuid: that.data.RoomGuid, //房间筛选选中值
        TaskStatus: that.data.TaskStatus, //保洁筛选选中状态
        Starttime: that.data.Starttime, //查询开始时间
        Endtime: that.data.Endtime, //查询开始时间
        TaskName: that.data.TaskName, //保洁任务名称
        tasklist: []
      })
      that.getHisTaskList();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})